// 张宇庭用的数据
import Mock from 'mockjs'
const data = Mock.mock({
  "list|30":[
    {
      "coupon_id|+1": 1, //优惠卷id
      "country|1": [{'name': '满10减2优惠卷', 'country_price': '2'}, {'name': '满20减6优惠卷', 'country_price': '6'}, {'name': '满50减14优惠卷', 'country_price': '14'}], //优惠卷
/*       "free_mail":"@free_mail()", //包邮形式 */
        "countryzj|1": ['有', '无'],//使用金额限制
       "country_time|1": ['7','14']
    }
]
})
const data1 = Mock.mock({
  "list|30":[
    {
      "goods_id|+1": 1, //商品id
      "goods_number|10000-99999": 1,//商品编号
      "goods_name|1": [{'name': '猪肉500g', 'type': '肉制品'}, {'name': '蛇肉500g', 'type': '肉制品'}, {'name': '牛肉500g', 'type': '肉制品'}, {'name': '土豆20kg', 'type': '蔬菜'}, {'name': '腐竹200g', 'type': '豆制品'}, {'name': '蘑菇20kg', 'type': '蔬菜'}, {'name': '豆腐200g', 'type': '豆制品'}], //商品名称和商品类型
      "goods_roucaicbprice|10-20": 1,
      "goods_roucaisprice|20-30": 1,
      "goods_kucun|30-50": 1,//库存
      "goods_yxiaoliang|100-150": 1,
      "goods_zxiaoliang|200-300": 1,
      "goods_yxiaoliangee|1000-3000": 1,
      "goods_zxiaoliangee|10000-30000": 1,
      "canyu_active|1": ['打折促销'],
        "rxboolean|1": ['是', '否'],
        "state|1": ['下架', '上架']
    }
]
})
// 通过Mock.mock函数模拟post请求
Mock.mock(RegExp('/coupon' + ".*"), 'get', function (option) {
  const query = decodeURIComponent(option.url).split('?')[1].split('&')[0].split('=')[1]
  const pagesize = option.url.split('?')[1].split('&')[2].split('=')[1]
    const pagenum = option.url.split('?')[1].split('&')[1].split('=')[1]
    const data2 = JSON.parse(JSON.stringify(data))
    let params = []
    let total = 0
    const number = pagenum * pagesize - data2.list.length
    const number1 = data2.list.length - (pagenum - 1) * pagesize
    if (query) {
     params = data2.list.filter(item => {
       return item.country.name.indexOf(query) !== -1
     }) 
     total = Number(params.length)    
     params = params.splice((pagenum - 1) * pagesize, pagesize)  
    } else {
      params = data2.list.splice((pagenum - 1) * pagesize, pagesize)
      number > 0 ? total = Number(data2.list.length) + Number(number1) : total = Number(data2.list.length) + Number(pagesize)
    }
    
    try {
      return Mock.mock({
        status: 200,
        message: '获取数据成功',
        params,
        total
      })
    } catch (err) {
      return Mock.mock({
        status: 444,
        message: '请求参数有误'
      })
    }
})

Mock.mock('/del', 'delete', function () {
  data.list.splice(0, 1)   
  try {
    return Mock.mock({
      status: 200,
      message: '获取数据成功'
    })
  } catch (err) {
    return Mock.mock({
      status: 444,
      message: '请求参数有误'
    })
  }
})
Mock.mock('/add', 'post', function (option) {
    const res = JSON.parse(option.body)
    const pagenum = res.pagenum
    const pagesize = res.pagesize
    data.list.push(res.content)
    const data2 = JSON.parse(JSON.stringify(data))
    let params = []
    let total = 0
    const number = pagenum * pagesize - data2.list.length
    const number1 = data2.list.length - (pagenum - 1) * pagesize
    params = data2.list.splice((pagenum - 1) * pagesize, pagesize)
    number > 0 ? total = Number(data2.list.length) + Number(number1) : total = Number(data2.list.length) + Number(pagesize)
  try {
    return Mock.mock({
      status: 200,
      message: '获取数据成功',
      params,
      total
    })
  } catch (err) {
    return Mock.mock({
      status: 444,
      message: '请求参数有误'
    })
  }
})

Mock.mock(RegExp('/goods' + ".*"), 'get', function (option) {
  let query = decodeURIComponent(option.url).split('?')[1].split('&')[0].split('=')[1]
  const pagesize = option.url.split('?')[1].split('&')[2].split('=')[1]
    const pagenum = option.url.split('?')[1].split('&')[1].split('=')[1]
    let data = []
    const total = data1.list.length
    let total1 = 0
    if (query) {
      query = JSON.parse(query)
      data1.list.forEach(item => {
        if (item.goods_name.name.indexOf(query.searchName) !== -1 && item.goods_name.type === query.type) {
          data.push(item)
        } else if (item.goods_number === Number(query.searchName) && item.goods_name.type === query.type) {
          data.push(item)
        }      
      })
      total1 = data.length
      data = data.slice((pagenum - 1) * pagesize, pagenum * pagesize)
     } else {
       data = data1.list.slice((pagenum - 1) * pagesize, pagenum * pagesize)
     }
  try {
    return Mock.mock({
      status: 200,
      message: '获取数据成功',
      data,
      data1,
      total,
      total1
    })
  } catch (err) {
    return Mock.mock({
      status: 444,
      message: '请求参数有误'
    })
  }
})

Mock.mock('/addGoods', 'post', function (option) {
  const res = JSON.parse(option.body)
  const pagenum = res.pagenum
  const pagesize = res.pagesize
  console.log(res.content)
  data1.list.push(res.content)
  let params = []
  let total = 0
  total = data1.list.length
  params = data1.list.slice((pagenum - 1) * pagesize, pagenum * pagesize)
try {
  return Mock.mock({
    status: 200,
    message: '获取数据成功',
    params,
    total
  })
} catch (err) {
  return Mock.mock({
    status: 444,
    message: '请求参数有误'
  })
}
})

Mock.mock('/delgoods', 'delete', function () {
  data1.list.splice(0, 1)   
  try {
    return Mock.mock({
      status: 200,
      message: '获取数据成功'
    })
  } catch (err) {
    return Mock.mock({
      status: 444,
      message: '请求参数有误'
    })
  }
})
